package com.ruizhong.centercontroll.util;

import org.cef.browser.CefBrowser;
import org.cef.browser.CefFrame;
import org.cef.browser.CefMessageRouter;
import org.cef.callback.CefQueryCallback;

import org.cef.handler.CefMessageRouterHandler;

public class MessageRouter {
    private CefMessageRouter messageRouter;

    public MessageRouter() {
        // 正确的方式：使用静态工厂方法创建
        this.messageRouter = CefMessageRouter.create();

        // 添加处理器
        this.messageRouter.addHandler(new CefMessageRouterHandler() {
            @Override
            public void setNativeRef(String s, long l) {

            }

            @Override
            public long getNativeRef(String s) {
                return 0;
            }

            @Override
            public boolean onQuery(CefBrowser browser, CefFrame frame, long queryId,
                                   String request, boolean persistent, CefQueryCallback callback) {
                System.out.println("Received from Vue: " + request);

                // 处理请求并返回响应
                callback.success("Processed: " + request);
                return true;
            }

            @Override
            public void onQueryCanceled(CefBrowser browser, CefFrame frame, long queryId) {
                System.out.println("Query cancelled: " + queryId);
            }
        }, true);
    }

    public CefMessageRouter getMessageRouter() {
        return messageRouter;
    }
}